package com.ce.cases.report.logic;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import com.ce.cases.report.dao.po.ReportTaxIllegal;
import com.ce.cases.source.dao.po.CaseSource;
import com.ce.cases.source.dao.po.CaseSourceDrawApprove;
import com.ce.cases.source.dao.po.DjNsrxx;
import com.ce.framework.datamaintain.implement.BaseDao;
import com.ce.framework.datamaintain.logic.ISQLMapper;
import com.ce.framework.util.GeneratorUUID;
import com.ce.util.DbUtil;

public class ReportIllegalLogic implements IReportIllegalLogic {
	private BaseDao basedao;
	
	@SuppressWarnings("unchecked")
	public String updatePublicSign(String[] vcId){
		StringBuffer hql=new StringBuffer("From ReportTaxIllegal po where po.vcDelFlag=0");
		if(vcId!=null){
			hql.append(" and po.vcId in(");
			for(int i=0;i<vcId.length;i++){
				if(i==0){
					hql.append("'"+vcId[i]+"'");
				}else{
					hql.append(",'"+vcId[i]+"'");
				}
				
			}
			hql.append(")");
		}
		try {
			List<ReportTaxIllegal> reportList = (List<ReportTaxIllegal>)this.basedao.findByHql(hql.toString());
			if(!reportList.isEmpty()){
				boolean isDeal=true;
				List<ReportTaxIllegal> updateStateList=new ArrayList<ReportTaxIllegal>();
				List<CaseSource>  caseSourceList=new ArrayList<CaseSource>();
				for(ReportTaxIllegal reportPo:reportList){
					reportPo.setState(3);
					reportPo.setStateInfo("已推送征管局");
					updateStateList.add(reportPo);
				}
				this.basedao.updatePos(updateStateList);
			}
		} catch (Exception e) {
			e.printStackTrace();
			return "推送操作出现异常！";
		}
		return "";
	}
	
	@SuppressWarnings("unchecked")
	public String dealSign(String[] vcId){
		StringBuffer hql=new StringBuffer("From ReportTaxIllegal po where po.vcDelFlag=0");
		if(vcId!=null){
			hql.append(" and po.vcId in(");
			for(int i=0;i<vcId.length;i++){
				if(i==0){
					hql.append("'"+vcId[i]+"'");
				}else{
					hql.append(",'"+vcId[i]+"'");
				}
				
			}
			hql.append(")");
		}
		try {
			List<ReportTaxIllegal> reportList = (List<ReportTaxIllegal>)this.basedao.findByHql(hql.toString());
			if(!reportList.isEmpty()){
				boolean isDeal=true;
				List<ReportTaxIllegal> updateStateList=new ArrayList<ReportTaxIllegal>();
				List<CaseSource>  caseSourceList=new ArrayList<CaseSource>();
				for(ReportTaxIllegal reportPo:reportList){
					if(reportPo.getDealResult()==2 && reportPo.getState()==0){
						reportPo.setState(2);
						reportPo.setStateInfo("已签收");
						updateStateList.add(reportPo);
						//写入备选案源库
						CaseSource source=new CaseSource();
						source.setVcId(GeneratorUUID.generateUUID());
						source.setRegisterPerson(reportPo.getRegisterPerson());
						source.setRegisterDeptId(reportPo.getRegisterDeptId());
						source.setRegisterDept(reportPo.getRegisterDept());
						source.setRegisterDate(reportPo.getRegisterDate());
						source.setTaxpayerNumber(reportPo.getTaxpayerNumber());
						source.setInvolvedEnterprice(reportPo.getTaxEnterprice());
						source.setInspectionDeptId(reportPo.getInspectionDeptId());
						source.setInspectionDept(reportPo.getInspectionDept());
						source.setCollectionDeptId(reportPo.getCollectionDeptId());
						source.setCollectionDept(reportPo.getCollectionDept());
						source.setCaseFromNum("8");
						source.setCaseFromName("举报");
						source.setState(0);
						source.setIsSecret(1);
						source.setReportId(reportPo.getVcId());
						source.setVcDelFlag(0);
						caseSourceList.add(source);
					}else{
						isDeal=false;
						break;
					}
				}
				if(isDeal){
					this.basedao.updatePos(updateStateList);
					this.basedao.addPos(caseSourceList);
				}else{
					return "存在不允许签收的记录！";
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			return "签收操作出现异常！";
		}
		return "";
	}
	
	public String dealDraw(String reportId,CaseSourceDrawApprove drawAppr){
		try {
			ReportTaxIllegal reportPo=(ReportTaxIllegal)this.basedao.getPo("com.ce.cases.report.dao.po.ReportTaxIllegal", reportId);
			if(reportPo.getDealResult()!=2 && reportPo.getState()==0){
				reportPo.setState(1);
				reportPo.setStateInfo("已提请");
				basedao.updatePo(reportPo);
				//写入备选案源库
				CaseSource source=new CaseSource();
				source.setVcId(GeneratorUUID.generateUUID());
				source.setRegisterPerson(reportPo.getRegisterPerson());
				source.setRegisterDeptId(reportPo.getRegisterDeptId());
				source.setRegisterDept(reportPo.getRegisterDept());
				source.setRegisterDate(reportPo.getRegisterDate());
				source.setTaxpayerNumber(reportPo.getTaxpayerNumber());
				source.setInvolvedEnterprice(reportPo.getTaxEnterprice());
				source.setInspectionDeptId(reportPo.getInspectionDeptId());
				source.setInspectionDept(reportPo.getInspectionDept());
				source.setCollectionDeptId(reportPo.getCollectionDeptId());
				source.setCollectionDept(reportPo.getCollectionDept());
				source.setCaseFromNum("8");
				source.setCaseFromName("举报");
				source.setState(0);
				source.setReportId(reportPo.getVcId());
				source.setVcDelFlag(0);
				this.basedao.addPo(source);
				//写入案源提请（移送）表
				drawAppr.setVcId(GeneratorUUID.generateUUID());
				drawAppr.setCaseId(source.getVcId());
				drawAppr.setVcDelFlag(0);
				this.basedao.addPo(drawAppr);
			}else{
				return "该检举记录不允许提请！";
			}
		}  catch (Exception e) {
			e.printStackTrace();
			return "提请操作出现异常！";
		}
		return "";
	}
	public DjNsrxx getDjNsrxxRemote(String taxpayerNumber){
		
		
		Connection con=null;
		try {
			con=DbUtil.getConnection(Class.forName("oracle.jdbc.driver.OracleDriver"), "jdbc:oracle:thin:@//98.12.102.73:1521/cqsthxqf", "wsbs_ysq", "wsbs_ysq");
		} catch (ClassNotFoundException e1) {
			e1.printStackTrace();
		} catch (Exception e1) {
			e1.printStackTrace();
		}
		Statement st=null;
		ResultSet rs=null;
		List<DjNsrxx> ll=new ArrayList<DjNsrxx>();
		if(con!=null){
			String sql="select t.* from hx_dj.dj_nsrxx t where t.NSRSBH='"+taxpayerNumber+"'";
			try {
				st = con.createStatement();
				rs = st.executeQuery(sql);
				
				while(rs.next()){
					DjNsrxx po=new DjNsrxx();
					try {
						po.setVcId(""+rs.getFloat("DJXH"));
						po.setNsrsbh(rs.getString("NSRSBH"));
						po.setNsrmc(rs.getString("NSRMC"));
						po.setFddbrhfzrhyzxm(rs.getString("FDDBRHFZRHYZXM"));
						po.setFddbrhfzrhyzsfzjhm(rs.getString("FDDBRHFZRHYZSFZJHM"));
						po.setZgswjDm(rs.getString("ZGSWJ_DM"));
					} catch (SQLException e) {
					}
					ll.add(po);
				}
				
			} catch (SQLException e) {
				e.printStackTrace();
			}finally{
				try {
					if(rs!=null)
						rs.close();
					if(st!=null)
						st.close();
					if(con!=null)
						con.close();
				} catch (SQLException e) {
					// TODO Auto-generated catch block
				}
				
			}
			
		}
		if(null!=ll&&ll.size()>0){
			return ll.get(0);
		}
		return null;
		
		
//		String sql="select * from EXPORT_TABLE t where t.NSRSBH='"+taxpayerNumber+"'";
//		List<DjNsrxx> l=DbUtil.executeSqlQuery(sql, new ISQLMapper(){			
//			@Override
//			public Object fromSQLQuery(ResultSet rs, int num) {
//				DjNsrxx po=new DjNsrxx();
//				try {
//					po.setVcId(""+rs.getFloat("DJXH"));
//					po.setNsrsbh(rs.getString("NSRSBH"));
//					po.setNsrmc(rs.getString("NSRMC"));
//					po.setFddbrhfzrhyzxm(rs.getString("FDDBRHFZRHYZXM"));
//					po.setFddbrhfzrhyzsfzjhm(rs.getString("FDDBRHFZRHYZSFZJHM"));
//					po.setZgswjDm(rs.getString("ZGSWJ_DM"));
//				} catch (SQLException e) {
//				}
//				return po;
//			}
//			
//		});
//		if(null!=l&&l.size()>0){
//			return l.get(0);
//		}
//		return null;
	}

	public BaseDao getBasedao() {
		return basedao;
	}

	public void setBasedao(BaseDao basedao) {
		this.basedao = basedao;
	}
	
	

}
